<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="prc.*" %>  
<jsp:include page="top.jsp"/>



<div id="page-body">

	
	
	<div class="panel">
		<div class="inner"><span class="corners-top"><span></span></span>
		<h2>Forum - Registration</h2>
		<fieldset class="fields2">
		<%
			String error="";
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String confirmPassword = request.getParameter("password_confirm");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			int age=0;
			
			
			session.setAttribute( "username", username);
			session.setAttribute( "firstname", firstname);
			session.setAttribute( "lastname", lastname);

			
			try{
				age=Integer.parseInt(request.getParameter("age"));
				session.setAttribute( "age", age);
			}catch (Exception e){
				error+="<dl><dd class=\"error\">Age must be a number</dd></dl>";
			}
			System.out.println(UserManagement.getUserByUsername(username));
			if(UserManagement.getUserByUsername(username)!=null){
				error+="<dl><dd class=\"error\">Username taken</dd></dl> ";
			}
			else{
				if(password.equals(confirmPassword)){
					if(password.length()<6){
						error+="<dl><dd class=\"error\">Password must have more then 6 characters</dd></dl> ";
					}
					else{
						User user=new User(username,password,firstname,lastname,age);
						UserManagement.addUser(user);
					}
					
				}
				else 
					error+="<dl><dd class=\"error\">Passwords don't match</dd></dl> ";
			}
							
			
				
			if(error.length()>0){
				out.println(error);
				 out.println("<a href=\"registration.jsp\" class=\"forumtitle\" >Go Back</a>");
			}
			else{
				out.println("Welcome, "+username+"<p><a href=\"index.jsp\" class=\"forumtitle\" >Go to index</a>");
				session.setAttribute( "username", "");
				session.setAttribute( "firstname", "");
				session.setAttribute( "lastname", "");
			}
		%>
		
	
		</fieldset>
	</div>
	<div class="panel">
		<div class="inner"><span class="corners-top"><span></span></span>
	
		<span class="corners-bottom"><span></span></span></div>
	</div>


</div>

<jsp:include page="bottom.jsp"/>